import axios from 'axios'
import qs from 'qs'

export async function getUserList(page, limit) {
    var resp = await axios.get("/api/petSystem/user.do", {
        params: {
            page,
            limit
        }
    });
    return resp.data.data;
}

export async function addUser(user) {
    var resp = await axios.get("/api/petsSystem/user.do", {
        params: {
            method: "addUser",
            userName: user.userName,
            userTel: user.userTel,
            userPwd: user.userPwd,
            userEmail: user.userEmail,
        }
    });
    return resp.data;
}

export async function updateUser(user) {
    var resp = await axios.get("/api/petsSystem/user.do", {
        params: {
            method: "updateUser",
            userId: user.userId,
            userName: user.userName,
            userTel: user.userTel,
            userPwd: user.userPwd,
            userEmail: user.userEmail,
        }
    });
    return resp.data;
}

export async function deleteUser(id) {
    var resp = await axios.get("/api/petsSystem/user.do", {
        params: {
            method: "deleteUser",
            userId: id
        }
    });
    return resp.data;
}
export async function queryUser(type, content, page, limit) {
    var resp = await axios.get("/api/petsSystem/user.do", {
        params: {
            method: "queryUser",
            type,
            content,
            page,
            limit
        }
    });
    return resp.data;
}

export async function login(user) {
    var resp = await axios.post("/api/petSystem/user.do",
        qs.stringify({
            method: "login",
            userName: user.userName,
            pwd: user.userPwd
        }));
    var token = resp.headers.authorization
    if (token != null || token !== "") {
        localStorage.setItem("token", token);
    }
    return resp.data;
}

export async function verify(){
    console.log("-----verify---------");
    var token = localStorage.getItem("token");
  
    if(token == null){
      return null;
    }
  
    var resp = await axios.get("/api/petSystem/user.do",{
      headers:{
        authorization: `Bearer ${token}`
      },
      params:{
        method:"verify"
      },
    });
  
    //因为后台返回了一个User对象，这个对象我们在验证成功之后需要赋值给loginUser仓库的
    //data,如果得到值为null，可能仓库在赋值的时候就会报错
    return resp.data
  } 
  
  export function loginOut(){
    localStorage.removeItem("token");
  }